package com.zhw.server;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;

import java.net.HttpCookie;
import java.nio.charset.Charset;

/**
 * @author weizhang52
 * @description:  自定义Handler
 * @date 2019/4/16
 * @modify
 */
@Slf4j
public class MyHandler extends SimpleChannelInboundHandler<HttpObject> {

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpObject httpObject) throws Exception {

        // 获取channel
        Channel channel=channelHandlerContext.channel();
        log.info("[MyHandler] 收到客户端的消息={}", httpObject.toString());
        log.info("[MyHandler] client addr = {}",channel.remoteAddress());
        if (httpObject instanceof HttpRequest){

            log.info("[MyHandler] HttpObject类型为HttpRequest");
        }

        // 定义发送的数据
        ByteBuf byteBufContent = Unpooled.copiedBuffer("Netty Res From Server",CharsetUtil.UTF_8);

        // 构建一个http响应
        FullHttpResponse httpResponse=new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,HttpResponseStatus.OK,byteBufContent);

        // 设置响应的数据类型和长度
        httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE,"text/plain");
        httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH,byteBufContent.readableBytes());

        // 返回给client
        channelHandlerContext.writeAndFlush(httpResponse);
        log.info("[MyHandler] 已返回客户端httpResponse={}",httpResponse);

    }

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {

        log.info("[MyHandler] channelRegistered");
        super.channelRegistered(ctx);
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        log.info("[MyHandler] channelUnregistered");
        super.channelUnregistered(ctx);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("[MyHandler] channelActive");
        super.channelActive(ctx);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        log.info("[MyHandler] channelInactive");
        super.channelInactive(ctx);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        log.info("[MyHandler] channelReadComplete");
        super.channelReadComplete(ctx);
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        log.info("[MyHandler] userEventTriggered");
        super.userEventTriggered(ctx, evt);
    }

    @Override
    public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
        log.info("[MyHandler] channelWritabilityChanged");
        super.channelWritabilityChanged(ctx);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.info("[MyHandler] exceptionCaught");
        super.exceptionCaught(ctx, cause);
    }

}
